import path from 'node:path'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // 按需导入ElementPlus
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  server: {
    host: '0.0.0.0',
    // 代理配置，用于重定向请求到其他服务器
    proxy: {
      // 定义一个代理规则，将/hello-world路径下的请求代理到指定的目标服务器
      '/gateway-api': {
        // 目标服务器的地址
        target: 'http://127.0.0.1:9999',
        // 更改请求的origin为代理服务器的origin，以便与目标服务器交互
        changeOrigin: true,
        // 重写请求路径，移除/gateway-api前缀
        rewrite: path => path.replace(/^\/gateway-api/, ''),
      },
      '/nacos': {
        target: 'http://192.168.30.129:8848/nacos/#/login', // Nacos 服务的实际地址
        changeOrigin: true,
        // 重写请求路径，移除/nacos前缀
        rewrite: path => path.replace(/^\/nacos/, ''),
      },
      '/console-ui': {
        target: 'http://192.168.30.129:8848/nacos/console-ui', // Nacos 服务的实际地址
        changeOrigin: true,
        // 重写请求路径，移除/console-ui前缀
        rewrite: path => path.replace(/^\/console-ui/, ''),
      },
      '/js': {
        target: 'http://192.168.30.129:8848/nacos/js', // Nacos 服务的实际地址
        changeOrigin: true,
        // 重写请求路径，移除/js前缀
        rewrite: path => path.replace(/^\/js/, ''),
      },
      '/v1': {
        target: 'http://192.168.30.129:8848/nacos/v1', // Nacos 服务的实际地址
        changeOrigin: true,
        // 重写请求路径，移除/js前缀
        rewrite: path => path.replace(/^\/v1/, ''),
      },
      '/img': {
        target: 'http://192.168.30.129:8848/nacos/img', // Nacos 服务的实际地址
        changeOrigin: true,
        // 重写请求路径，移除/js前缀
        rewrite: path => path.replace(/^\/img/, ''),
      },
      '/css': {
        target: 'http://192.168.30.129:8848/nacos/css', // Nacos 服务的实际地址
        changeOrigin: true,
        // 重写请求路径，移除/js前缀
        rewrite: path => path.replace(/^\/css/, ''),
      },
    },
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        additionalData: '@use "@/styles/variable.scss" as *;',
      },
    },
  },
})
